package crm.base.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.ModelAndView;

import crm.base.model.BaseModel;

public class BaseController {

	protected ModelAndView forword(String viewName, BaseModel model) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject(model);
		modelAndView.setViewName(viewName);
		return modelAndView;
	}

	protected ModelAndView forword(String viewName, Map<String, Object> model) {
		return new ModelAndView(viewName, model);
	}

	protected ModelAndView forword(String viewName, String modelName, Object modelObject) {
		return new ModelAndView(viewName, modelName, modelObject);
	}
	
	protected ModelAndView redirect(String viewName, Map<String, Object> model) {
		return new ModelAndView("redirect:" + viewName, model);
	}
	
//	protected ModelAndView redirect(String viewName, Map<String, Object> model) {
//		return new ModelAndView(new RedirectView(viewName), model);
//	}
	

	/**
	 * 获取IP地址
	 * 
	 * @param request
	 * @return
	 */
	protected String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
